package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class srv_notasEstadistica extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Connection cn = null;
        Map estadistica = new HashMap();
        HttpSession session = request.getSession(true);
        try {
            String curso = request.getParameter("curso");
            response.setContentType("text/html");
            ArrayList<Map> lstAlumnosNot = new ArrayList<Map>();
            // conectamos la BD 
            GestionBD og = new GestionBD();
            cn = og.conectar();
            String query = "select max(not_promfinal),min(not_promfinal),avg(not_promfinal) from nota where id_curso="+curso;
            Statement stm = cn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            while (rs.next()) {
                // obtenemos un curso
                estadistica.put("maximo", rs.getInt("max(not_promfinal)"));
                estadistica.put("minimo", rs.getInt("min(not_promfinal)"));
                estadistica.put("promedio", rs.getInt("avg(not_promfinal)"));
                // lo almacenamos en el array
            }
            query = "select count(not_promfinal) from nota where not_promfinal>11 and id_curso="+curso;
            stm = cn.createStatement();
            rs = stm.executeQuery(query);
            while (rs.next()) {
                // obtenemos un curso
                estadistica.put("aprobado", rs.getInt("count(not_promfinal)"));
                // lo almacenamos en el array
            }
            query = "select count(not_promfinal) from nota where not_promfinal<11 and id_curso="+curso;
            stm = cn.createStatement();
            rs = stm.executeQuery(query);
            while (rs.next()) {
                // obtenemos un curso
                estadistica.put("desaprobado", rs.getInt("count(not_promfinal)"));
                // lo almacenamos en el array
            }
            session.setAttribute("estadistica", estadistica);
            stm.close();
            //cn.close();
        } catch (Exception e) {
            System.out.println("problemas al obtener estadistica");
        }
        // generamos la vista y lo devolvemos al cliente
        RequestDispatcher rd = request.getRequestDispatcher("listaEstadistica.jsp");
        rd.forward(request, response);
    }
}
